import React from 'react';

import {Select} from 'antd';

import styles from './index.less';

export default class SelectEx extends React.Component{

  state = {
    value:""
  }

  onValueChange(e){

    var validate = this.props.validate;
    this.props.emitter.emit('onValueChange', this.props.name, e);
    this.setState({
      value:e
    });

    if (this.props.onValueChange){
      this.props.onValueChange(e);
    }
  }

  iemi = null;

  reset(){
    this.setState({
      value:this.props.defaultValue
    })
  }

  componentWillReceiveProps(n){
  }

  componentWillMount(){
    this.reset();

    this.iemi = this.props.emitter.addListener('onReset', ()=>{this.reset()});
    this.props.emitter.emit('onValueChange', this.props.name, this.props.defaultValue);
  }
  
  componentWillUnmount(){
    this.iemi.remove();
  }

  render(){
    return <Select disabled = {this.props.disabled} 
                   name = {this.props.name} 
                   type = {this.props.type} 
                   style = {this.props.style?this.props.style:{width:120}}
                   value = {this.state.value} 
                   onChange = {(e) => {
                    this.onValueChange(e);
                  }}
    >
      {this.props.children}
    </Select>
  }
}